//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.PlugIns.TemporaryStorage {
    /// <summary>A <see cref="TemporaryStorage"/> that stores data in a temporary file on disk.</summary>
    /// <remarks>Use this store when the results are too big to store in memory.  <see cref="MemoryStorage"/> is faster but take up much more memory.</remarks>
    public class FileStorage : Eas.WebCrawler.PlugIns.TemporaryStorage.StreamBasedStorage{
        
        /// <summary>Creates a <see cref="FileStorage"/> from a name for the temporary file to use.</summary>
        /// <param name="tempFileName"></param>
        public FileStorage(string tempFileName) {
            _TempFileName = tempFileName;
        }

        string _TempFileName;

        /// <summary>The name of the temporary file to use.</summary>
        public string FileName { get { return _TempFileName; } }

        /// <summary>Disposes of this instance</summary>
        /// <param name="isDisposing">Whether we are disposing or finalizing.</param>
        protected override void Dispose(bool isDisposing) {
            try {
                if (isDisposing) {
                    if (System.IO.File.Exists(_TempFileName))
                        System.IO.File.Delete(_TempFileName);
                }
            }
            finally {
                base.Dispose(isDisposing);
            }
        }

        /// <summary>Get the underlying file stream for reading.</summary>
        /// <returns>The underlying file stream.</returns>
        protected override System.IO.Stream GetStreamForReading() {
            return new System.IO.FileStream(_TempFileName, System.IO.FileMode.Open);            
        }

        /// <summary>Get the underlying file stream for writting.</summary>
        /// <returns>The underlying file stream.</returns>
        protected override System.IO.Stream GetStreamForWriting() {
            return new System.IO.FileStream(_TempFileName, System.IO.FileMode.Create);
        }
    }

    /// <summary>A <see cref="TemporaryStorageFactory"/> that creates a <see cref="FileStorage"/> to store data in.</summary>
    /// <remarks>Use this store factory when the results are too big to store in memory.  <see cref="MemoryStorageFactory"/> is faster but take up much more memory.</remarks>
    public class FileStorageFactory : Eas.WebCrawler.Interfaces.TemporaryStorageFactory {

        /// <summary>The Directory where temporary files are created.</summary>
        /// <remarks>Backing field for the StorageDirectory property.</remarks>
        /// <seealso cref="StorageDirectory"/>
        private System.IO.DirectoryInfo _StorageDirectory;

        /// <summary>The Directory where temporary files are created.</summary>
        public System.IO.DirectoryInfo StorageDirectory {
            get { return _StorageDirectory; }
        }

        /// <summary>Constructs a <see cref="FileStorageFactory"/> from the directory in which to store the temporary files.</summary>
        /// <param name="storageDirectory">The directory in which to store the temporary files.</param>
        public FileStorageFactory(string storageDirectory) {
            _StorageDirectory = new System.IO.DirectoryInfo(storageDirectory);
            if (!_StorageDirectory.Exists)
                throw new ArgumentOutOfRangeException("storageDirectory", "The specified directory does not exist.");
        }

        #region TemporaryStorageFactory Members

        /// <summary>Creates a new instance of a <see cref="FileStorage"/> that points to a temp file.</summary>
        /// <param name="processingState">The <see cref="ProcessingState"/> for the current request.</param>
        /// <returns>A new instance of a <see cref="FileStorage"/>.</returns>
        public Eas.WebCrawler.Interfaces.TemporaryStorage Create(ProcessingState processingState) {
            return new Eas.WebCrawler.PlugIns.TemporaryStorage.FileStorage(System.IO.Path.Combine(_StorageDirectory.FullName, new Guid().ToString() + ".tmp"));   
        }

        #endregion

        #region WebCrawlerComponent Members

        /// <summary>Initializes this class</summary>
        /// <param name="context">The <see cref="WebCrawlerContext"/> which will contain data about this instance of the web crawler.</param>
        public void Initialize(Eas.WebCrawler.Interfaces.WebCrawlerContext context) {
        }
                
        #endregion
    }
}
